//
//  PhysicObject.m
//  Box2dDemo
//
//  Created by Thi Huynh on 9/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PhysicObject.h"

@implementation PhysicObject

@synthesize objectBody      = _objectBody;
@synthesize objectSprite    = _objectSprite;
@synthesize name            = _name;
@synthesize parentLayer     = _parentLayer;
@synthesize pos             = _pos;
@synthesize direction       = _direction;
@synthesize scale           = _scale;
@synthesize tag             = _tag;

#pragma mark - dinit section

- (id)initObjectWithWorld:(b2World*)world pos:(CGPoint)pos imgPath:(NSString*)imgPath
{
    self  = [super init];
    if(self)
    {
        _name = imgPath;
        _pos = pos;
        [self createBodyWith:world];
        [self createSpriteWithFileName:imgPath];
    }
    
    return  self;
}
/****************************************************
/create sprite for object with file name
****************************************************/
- (void)createSpriteWithFileName:(NSString*)fileName
{
    _objectSprite = [CCSprite spriteWithFile:fileName];
}
/****************************************************
/create physic body for object
****************************************************/
- (void)createBodyWith:(b2World*)world
{
    
}
/****************************************************
/create something for object peoperties
****************************************************/
- (void)startUp
{
    _tag = UNDEFINE_TAG;
}

#pragma mark - get set section
/****************************************************
/set transform position from sprite to body
****************************************************/
- (void)setTransformPos:(CGPoint)point
{
    [_objectSprite setPosition:point];
    if(_objectBody)
    {
        b2Vec2 boxPosition = b2Vec2(point.x/PTM_RATIO,point.y/PTM_RATIO);
        float angle = CC_DEGREES_TO_RADIANS(-1*_objectSprite.rotation);
        _objectBody->SetTransform(boxPosition, angle);
    }
}
/****************************************************
/set transform rotation from sprite to body
****************************************************/
- (void)setTransformRot:(float)angle
{
    [_objectSprite setRotation:angle];
    if(_objectBody)
    {
        b2Vec2 boxPosition = b2Vec2(_objectSprite.position.x ,_objectSprite.position.y);
        float rot = CC_DEGREES_TO_RADIANS(-1*angle);
        _objectBody->SetTransform(boxPosition, rot);
    } 
}
#pragma mark - update section
- (void)update:(ccTime)dt
{
    //TODO: update object properties , state
}
#pragma mark - other method

#pragma mark - dealloc section
- (void)dealloc
{
    if(_objectSprite)
    {
        [_objectSprite release];
        _objectSprite = nil;
    }
    if(_name)
    {
        [_name release];
        _name = nil;
    }
    [super dealloc];
    
}

@end
